package com.seata.order.controller;

import com.alibaba.fastjson.JSON;
import com.seata.order.pojo.Message;
import com.seata.order.pojo.Order;
import com.seata.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * @Description:
 * @ClassName: OrderController
 * @Author:
 * @Date:
 * @Version: 1.0
 */
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    public RestTemplate restTemplate;
    @GetMapping("/addOrder")
    @GlobalTransactional
    public Message addOrder(){
        //模拟接收前端的订单信息
        Order order = new Order();
        order.setProductId(123);
        order.setTotalAmount(200);
        order.setStatus(1);
        //调用订单服务
        System.out.println("订单:"+ orderService.addOrder(order));
        //更新库存
        HashMap<Object, Object> stock = new HashMap<>();
        stock.put("productId", order.getProductId());
        stock.put("count", order.getTotalAmount());
        Message forObject = restTemplate.postForObject("http://localhost:8002/updateStock", stock, Message.class);
        if (forObject.getErrorNo() == 99) {
            int a = 1/0;
        }
        System.out.println("调用库存服务：" + forObject);
        return new Message(0, "success", order);
    }
}
